package chapter07

import java.text.SimpleDateFormat

import org.apache.spark.{SparkConf, SparkContext}

/**
 * author: 余辉
 * blog: https://blog.csdn.net/silentwolfyh
 * descriptions:
 * date: 2024 - 09 - 02 11:33 上午
 */
object DateUtilClass {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName("WordCount")
      .setMaster("local[*]") //本地模式，开多个线程
    //1.创建SparkContext
    val sc = new SparkContext(conf)

    val lines = sc.textFile("doc/input/06.txt")

    val timeRDD = lines.mapPartitions(it => {
      //一个Task使用自己单独的DateUtilClass实例，缺点是浪费内存资源
      val dateUtil = new DateUtilClass
      it.map(e => {
        dateUtil.parse(e)
      })
    })

    val res = timeRDD.collect()
    println(res.toBuffer)

  }
}

class DateUtilClass {

  val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

  def parse(str: String): Long = {
    //2022-05-23 11:39:30
    sdf.parse(str).getTime
  }
}